﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Reflection;
using KP_CommonLibrary.DesignTime;
using System.Windows.Forms.Design;
using System.ComponentModel.Design;

using System.IO;
using DevComponents.DotNetBar.Controls;
using System.Drawing;

namespace KP_CommonLibrary.KP_CoreUI
{
    public class ChoiceFieldEditor : UITypeEditor
    {

        public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
        {
            if (context != null)
                return UITypeEditorEditStyle.DropDown;
            return base.GetEditStyle(context);
        }
        public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
        {

            if (context != null && provider != null)
            {
                
                UC_ChoiceField ctrl = null;
                Control instance = context.Instance as Control;
                try
                {


                    string tableName = string.Empty;
                   // Form f = (context.Instance as Control).FindForm();
                    ContainerControl container = null;
                    IContainerDataSource ic= FindContainer(context.Instance as Control);
                    container = ic as ContainerControl;
                    if (container == null)
                        return base.EditValue(context, provider, value);                   
                    KP_BindingController kp_binding = container.Container.Components["kP_BindingController"] as KP_BindingController;
                    if (kp_binding != null)
                    {

                        int i = kp_binding.GetKPSourceIndex(context.Instance as Control);

                        if (kp_binding.RelationTables != null && kp_binding.RelationTables.Length > i)
                        {
                            tableName = kp_binding.RelationTables[i];

                            IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                            ctrl = new UC_ChoiceField();
                            ctrl.EditorService = editorService;
                            ctrl.OlderField = value != null ? value.ToString() : "";
                            // if (tableName != string.Empty)
                            ctrl.DataBind(tableName);
                            editorService.DropDownControl(ctrl);
                            string newSelected = ctrl.SelectedField;

                            if (value == null && string.IsNullOrEmpty(newSelected))
                                return value;
                            else
                            {
                                if (!string.IsNullOrEmpty(newSelected))
                                {
                                    if (!ctrl.IsNullable && kp_binding.GetKPRequirement(instance) == false)
                                    {
                                        kp_binding.SetKPRequirement(instance, true);
                                        if (string.IsNullOrEmpty(kp_binding.GetKPMessage(instance)))
                                            kp_binding.SetKPMessage(instance, "Phải nhập giá trị cho " + newSelected);
                                    }
                                    if (instance is TextBox)
                                    {
                                        (instance as TextBox).MaxLength = ctrl.MaxLength;
                                        //using (Graphics g = instance.CreateGraphics())
                                        //{
                                        //   SizeF s= g.MeasureString("a".PadLeft(ctrl.MaxLength,'A'), instance.Font);
                                        //   if (instance.Size.Width < s.Width + 1)
                                        //       MessageBox.Show("Control " + instance.Name + " cần độ rộng tối thiểu " + s.Width.ToString());
                                        //}
                                    }
                                    if (instance is TextBoxX)
                                        (instance as TextBoxX).MaxLength = ctrl.MaxLength;
                                }
                            }

                            return ctrl.SelectedField;
                        }
                    }


                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message);
                }
                finally
                {
                    if (ctrl != null)
                        ctrl.Dispose();
                }
            }
            return base.EditValue(context, provider, value);
        }
        
        IContainerDataSource FindContainer(Control c)
        {
            while (true)
            {
                if (c is IContainerDataSource)
                    return c as IContainerDataSource;
                else
                    c = c.Parent;
                if (c == null)
                    return null;
            }
        }
    }
    public class RelationTableAttribute : Attribute
    {
        public string[] Tables { get; set; }
        public RelationTableAttribute(string[] tables)
        {
            Tables = tables;
        }
    }
}
